#ifndef __CONNECTION_H__
#define __CONNECTION_H__

#define CONNECTION_RECV_BUF_MAX 20480

class Connection
{
public:
	// ctor
	Connection( struct sockaddr_in src_addr );

	// dtor
	virtual ~Connection();

	// init
	// store incoming_sock
	// create outgoing_sock
	int Init( const int incoming_sock );

	// set read fds
	int SetReadFds( fd_set* p_read_fds, int* p_max_read_fd );
	
	// process connection
	// check if need to read incoming/outgoing
	// check if need to send incoming/outgoing
	int Process( fd_set* p_read_fds );

protected:
	// do outgoing connect (proxy->server)
	virtual int DoConnect();

	// send/recv (process bufs & call DoSend/DoRecv)
	int Send( const int sock, char* buf, int* p_data_len );
	int Recv( const int sock, char* buf, int* p_data_len );

	// virtual send/recv function (calls the real send/recv function)
	virtual int DoSend( const int sock, const char* data, const int len );
	virtual int DoRecv( const int sock, char* buf, const int len );

	// socket
	int m_incoming_sock;
	int m_outgoing_sock;
	
	// bufs
	char* m_incoming_recv_buf;
	char* m_outgoing_recv_buf;
	
	// buf size
	int m_incoming_recv_buf_len;
	int m_outgoing_recv_buf_len;
	
	// original outgoing target
	struct sockaddr_in m_original_outgoing_destination;
	
	// store src address
	struct sockaddr_in m_src_addr;
};

#endif /* __CONNECTION_H__ */
